package com.wlyuan.open.scanner.domain.order;

import com.wlyuan.open.scanner.dao.entity.OrderIndexDO;
import com.wlyuan.open.scanner.domain.attach.Attach;
import com.wlyuan.open.scanner.domain.order.mapping.*;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class OrderConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = {MappingInvalid.class})
    public interface Convert {
        @Mappings({
                @Mapping(target = "id", source = "indexId"),
                @Mapping(target = "info.status", source = "indexStatus"),
                @Mapping(target = "sourceKey", source = "indexId"),
                @Mapping(target = "createTime", source = "indexCreatetime"),
                @Mapping(target = "updateTime", source = "indexStatustime"),
                @Mapping(target = "tenantId", source = "indexCreatorcompanyid"),

                @Mapping(target = "info.srcOrderId", source = "indexSrcorderid"),
                @Mapping(target = "info.rootOrderId", source = "indexRootorderid"),
                @Mapping(target = "info.type", source = "indexSrcclass"),
                @Mapping(target = "info.orderNo", source = "indexCode"),
                @Mapping(target = "info.trackType", source = "indexTracktype"),
                @Mapping(target = "info.deviceCode", source = "indexDevicecode"),
                @Mapping(target = "info.days", source = "days"),
                @Mapping(target = "info.customerTenantId", source = "downTenantId"),

                @Mapping(target = "info.signTime", source = "indexSigntime"),
                @Mapping(target = "info.receiptSignTime", source = "indexVerifysigintime"),
                @Mapping(target = "info.signInfo", source = "indexSigninfo"),
                @Mapping(target = "info.create", source = "indexCreator"),
                @Mapping(target = "info.createManDesc", source = "createManDesc"),
                @Mapping(target = "info.createTime", source = "indexCreatetime"),
                @Mapping(target = "info.singer", source = "indexSinger"),
                @Mapping(target = "info.receiptTime", source = "indexReceiptime"),
                @Mapping(target = "info.receiptBy", source = "receiptBy"),
                @Mapping(target = "info.closeAt", source = "closeAt"),
                @Mapping(target = "info.closeBy", source = "closeBy"),
                @Mapping(target = "info.createType", source = "indexCreatetype"),
                @Mapping(target = "info.signType", source = "signType"),
                @Mapping(target = "info.signDesc", source = "signDesc"),
                @Mapping(target = "info.receiptDesc", source = "indexException"),
                @Mapping(target = "info.closeDesc", source = "indexClosement"),
                @Mapping(target = "info.delayReason", source = "delayReason"),

                @Mapping(target = "info.signQuantity", source = "signQuantity"),
                @Mapping(target = "info.signExceptionQuantity", source = "signExceptionQuantity"),
                @Mapping(target = "info.receiptQuantity", source = "receiptQuantity"),
                @Mapping(target = "info.receiptExceptionQuantity", source = "receiptExceptionQuantity"),

                @Mapping(target = "info.exceptionStatus", source = "exceptionStatus"),

                @Mapping(target = "orderReceiptDevice.orderId", source = "indexId"),
                @Mapping(target = "orderReceiptDevice.tenantId", source = "indexCreatorcompanyid"),
                @Mapping(target = "orderReceiptDevice.generateCert", source = "generateCert"),
                @Mapping(target = "orderReceiptDevice.score", source = "indexAssesslevel"),
                @Mapping(target = "orderReceiptDevice.scoreDesc", source = "indexAssessment"),
                @Mapping(target = "orderReceiptDevice.arrive", expression = "java(null)"),
                @Mapping(target = "orderReceiptDevice.realFromTime", source = "indexRealfromtime"),
                @Mapping(target = "orderReceiptDevice.realToTime", source = " indexRealtotime"),
                @Mapping(target = "orderReceiptDevice.smsFromTime", source = "indexStartmsgtime"),
                @Mapping(target = "orderReceiptDevice.smsToTime", source = "indexArrivemsgtime"),
                @Mapping(target = "orderReceiptDevice.deviceBindTime", source = "indexDevicebindingtime"),
                @Mapping(target = "orderReceiptDevice.estimateArriveTime", expression = "java(null)"),

                @Mapping(target = "attachments", source = "attachment"),

        })
        @BaseMapping
        @InfoMapping
        @FromMapping
        @ToMapping
        @PriceMapping
        @VehicleMapping
        @RequirementMapping
        Order convert(OrderIndexDO source);

        @AfterMapping
        default void after(@MappingTarget Order target, OrderIndexDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Order> convert(List<OrderIndexDO> list);

        @Mappings({
                @Mapping(target = "source", source = "syncThisTag"),
        })
        OrderAttach convertAttach(Attach source);

        List<OrderAttach> convertAttach(List<Attach> source);

    }

    public static List<Order> convert(List<OrderIndexDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Order convert(OrderIndexDO source) {
        return CONVERT.convert(source);
    }

    public static OrderAttach convertAttach(Attach source) {
        return CONVERT.convertAttach(source);
    }

    public static List<OrderAttach> convertAttach(List<Attach> source) {
        return CONVERT.convertAttach(source);
    }


}
